#include "membackend/accurateTransaction.h"

Transaction::Transaction()
{
    id = 0;
    address = 0;
    type = TRANSACTION_TYPE::READ; // Default type

    this->bank_id = 0;
    this->rank_id = 0;
    this->bank_group_id = 0;
    this->start_time = 0;
    this->end_time = 0;
    this->latency = 0;
}

Transaction::Transaction(uint64_t id, uint64_t address, TRANSACTION_TYPE type, const AddressMappingConfig *amp)
{
    this->id = id;
    this->address = address;
    this->type = type;


// Extract address components based on the address mapping configuration

    //地址映射
    std::cout << "Address before shift: " << std::hex << address << std::endl;
    std::cout << "Shift bits: " << amp->getShiftBits() << std::endl;
    address >>= amp->getShiftBits();
    std::cout << "Address after shift: " << std::hex << address << std::endl;

    // this->line_id = (address >> amp->getLineOffset()) & amp->getLineMask();
    this->column_id = (address >> amp->getColumnOffset()) & amp->getColumnMask();
    this->row_id = (address >> amp->getRowOffset()) & amp->getRowMask();
    this->bank_id = (address >> amp->getBankOffset()) & amp->getBankMask();
    this->bank_group_id = (address >> amp->getBankGroupOffset()) & amp->getBankGroupMask();
    this->rank_id = (address >> amp->getRankOffset()) & amp->getRankMask();

    std::cout << "Transaction created with ID: " << id 
              << ", Address: " << std::hex << address
              << ", Type: " << static_cast<int>(type) 
              << ", Bank ID: " << bank_id 
              << ", Rank ID: " << rank_id 
              << ", Bank Group ID: " << bank_group_id 
              << ", Row ID: " << row_id
                << ", Column ID: " << column_id
              << std::endl;
    

    this->start_time = 0;
    this->end_time = 0;
    this->latency = 0;
}

